package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.StringUtil;
import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.xelavos.client.ChangeableImage.ChangeableImageModel;

public class ChangeableImageController {

	private IImage changeableImage;
	private ChangeableImageModel changeableImageModel;

	public void registerChangeableImageModel(ChangeableImageModel changeableImageModel) {
		this.changeableImageModel = changeableImageModel;
		changeableImageModel.addPropertyChangeListener("url", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateUrl();
			}
		});
	}

	public void updateUrl() {
		String url = changeableImageModel.getUrl();
		changeableImage.setVisible(!StringUtil.isEmpty(url));
		if(url != null)
			changeableImage.setUrl(url);
	}

	public void registerChangeableImage(IImage changeableImage) {
		this.changeableImage = changeableImage;
	}

}
